var handleVideoFilesStep=
{
	title:'Handle Video Files',
	createUI:function()
	{
		var ret=$('<div>'+
			'Video files<br>'+
			'<table id="tblVideoFiles">'+
			'<thead><tr><th>Filename</th><th>Original Size</tr><th>Compressed Size</th></thead><tbody></tbody>'+
			'</table>'+
			'<button id="btnRefreshlVideoFiles">Refresh</button>'+
			'<button id="btnCompressAllVideoFiles">Compress All</button>'+
			'</div>');
		return ret;
	},
	postCreateUI:function()
	{
		$('#txtAlbumPath').change(function(){
			handleVideoFilesStep.refresh();
		});
		$('#btnCompressAllVideoFiles').click(function(){
			handleVideoFilesStep.compressAll();
		});
		$('#btnRefreshlVideoFiles').click(function(){
			handleVideoFilesStep.refresh();
		});
		
	},
	refresh:function(){
		
		$('#tblVideoFiles tbody tr').remove();
		
		var folderObj;
		try{
			folderObj=File.objFSO.GetFolder($('#txtAlbumPath').val());
		}catch(e){
			return;
		}
		
		var fc = new Enumerator(folderObj.files);
		for (; !fc.atEnd(); fc.moveNext())
		{
			var filePath=''+fc.item();
			var fileName=filePath.substring(filePath.lastIndexOf('\\')+1);
			
			if(!/.avi/i.exec(fileName))
				continue;
			
			var itemTemplate='<tr><td class="tdFileName"></td><td class="tdOriginalSize"></td><td class="tdCompressedSize"></td></tr>';
			
			if(fileName.indexOf('.compressed.')>0){
				//this is a compressed file
				var newRow=$(itemTemplate);
				newRow.children('.tdFileName').text(fileName);
				newRow.children('.tdCompressedSize').text(File.objFSO.GetFile(filePath).Size);
				newRow.get(0).info=
					{
						compressedFilePath:filePath
					};
				
				$('#tblVideoFiles tbody').append(newRow);
			}else if(fileName.indexOf('.original')>0){
				//this is the original file
			}else{
				//this file is not handled before
				var newRow=$(itemTemplate);
				newRow.children('.tdFileName').text(fileName);
				newRow.children('.tdOriginalSize').text(File.objFSO.GetFile(filePath).Size);
				newRow.get(0).info=
					{
						originalFilePath:filePath
					};
				
				$('#tblVideoFiles tbody').append(newRow);
			}
			
			//$('#foundItems').append($('<option selected>'+fc.item()+'</option>'));
		}
		
	},
	compressAll:function(){
		$('#tblVideoFiles tbody tr').each(function(){
			var info=$(this).context.info;

			if(info.compressedFilePath==null){
				//compress the file
				
				//calculate the compressed file path
				var p=info.originalFilePath.lastIndexOf('.');
				var compressedFilePath=info.originalFilePath.substr(0,p)+'.compressed'+info.originalFilePath.substr(p);
				
				var vdubPath='D:\\Green\\VirtualDub\\vdub.exe';
				var cmd=vdubPath+' /c /s "'+Environment.dataPath+'\\qualityBased.10.vcf" /p "'+info.originalFilePath+
					'" "'+ compressedFilePath +'" /r /x';
				
				var oExec=Shell.obj.Exec(cmd);
				var out=oExec.StdOut.ReadAll();
				var err=oExec.StdErr.ReadAll();
				
				File.objFSO.MoveFile(info.originalFilePath,info.originalFilePath+'.original');
							
			}
		});
		handleVideoFilesStep.refresh();
	}
	
};
	
stepManager.add(handleVideoFilesStep);
